//
//  UIViewForActivityIndicator.m
//  Breadtrip
//
//  Created by liushuai on 16/3/12.
//  Copyright © 2016年 liushuai1992@gmail.com. All rights reserved.
//

#import "UIViewForActivityIndicator.h"
#import "AppMacro.h"
@interface UIViewForActivityIndicator ()

@property (nonatomic, retain) UIView *view;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;

@end

@implementation UIViewForActivityIndicator

#pragma mark - dealloc

- (void)dealloc
{
    [_view release];
    [_activityIndicator release];
    [super dealloc];
}

#pragma mark - singleton pattern
+ (instancetype)shareActivityIndicatorViewWithFrame:(CGRect)frame {
    static UIViewForActivityIndicator *activityIndicatorView = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        activityIndicatorView = [[UIViewForActivityIndicator alloc] initWithFrame:frame];
    });
    return activityIndicatorView;
}


#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createViews];
    }
    return self;
}

#pragma mark - createView
- (void)createViews {
    /** view */
    self.view = [[UIView alloc] init];
    [self addSubview:self.view];
    
    [_view release];
    
    /** activityIndicator */
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activityIndicator];
    self.alpha = 0;
    [_activityIndicator release];
    [self.activityIndicator startAnimating];
}


#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    self.alpha = 0;
    /** view */
    self.view.frame = CGRectMake(self.frame.size.width / 5 * 2, self.frame.size.height / 2 - self.frame.size.height / 5 / 2 + (64 - 49),self.frame.size.width / 5, self.view.frame.size.width);
    self.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4f];
    self.view.layer.cornerRadius = 5;
    self.view.layer.masksToBounds = YES;
    
    /** activityIndicator */
    self.activityIndicator.frame = self.view.bounds;

    [self performSelector:@selector(displayTheView) withObject:nil afterDelay:0.3f];
}

- (void)displayTheView {
    self.alpha = 1;
}

#pragma mark - finish
- (void)didFinishLoad {
    [self removeFromSuperview];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
